package com.dream.inspect.modules.job.common;


import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.modules.job.entity.ScheduleJobEntity;
import com.dream.inspect.modules.job.service.ScheduleJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 定时任务调用方法
 */
@Controller
public class ScheduleCommonMethod{
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ScheduleJobService scheduleJobService;

    public void addTimeMessage(Date date,String type, String params){
        ScheduleJobEntity scheduleJob = new ScheduleJobEntity();

        switch(type){
            case "1"://限时整改剩余36小时
                scheduleJob.setBeanName("sendMessageTask");
                scheduleJob.setMethodName("sendQuestionEnd");
                scheduleJob.setCronExpression(getCronByDate(getNewDateByMinusHour(date,-36)));
                scheduleJob.setParams(params);//"questionId,deptId"
                break;
            case "2"://申请专项到截止日期审核人员还未操作
                scheduleJob.setBeanName("rejectProjectTask");
                scheduleJob.setMethodName("rejectProject");
                scheduleJob.setCronExpression(getCronByDate(date));
                scheduleJob.setParams(params);//专项Id
                break;
            case "3"://通过专项时
                //专项截止前36小时需发送提醒消息
                scheduleJob.setBeanName("sendMessageTask");
                scheduleJob.setParams(params);//专项Id
                if(getNewDateByMinusHour(date,-36).getTime() > new Date().getTime()){
                    scheduleJob.setMethodName("sendProjectEnd36");
                    scheduleJob.setCronExpression(getCronByDate(getNewDateByMinusHour(date,-36)));
                    insertScheduleJob(scheduleJob);
                }
                //专项截止时需发送提醒消息
                scheduleJob.setMethodName("sendProjectEnd");
                scheduleJob.setCronExpression(getCronByDate(date));
                break;
            default:
                logger.info("无此类型");
                return;
        }
        insertScheduleJob(scheduleJob);
    }

    public void insertScheduleJob(ScheduleJobEntity scheduleJob){
        ValidatorUtils.validateEntity(scheduleJob);
        scheduleJobService.save(scheduleJob);
    }

    /**
     * 得到减去一个时间后的新日期
     * @param date 日期
     * @param hour 小时 减传-1  加 1
     * @return
     */
    public Date getNewDateByMinusHour(Date date,int hour){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR,hour);
        return cal.getTime();
    }

    /**
     * 得到减去一个时间后的新日期
     * @param str 日期
     * @param hour 小时 减传-1  加 1
     * @return
     */
    public Date getNewDateByMinusHour(String str,int hour){
        Calendar cal = Calendar.getInstance();
        try {
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date= sdf.parse(str);
            cal.setTime(date);
            cal.add(Calendar.HOUR,hour);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.getTime();
    }

    /**
     * 传入时间 返回corn 表达式
     * @param date
     * @return
     */
    public String getCronByDate(Date date){
        String corn = "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);//获取年份
        int month=cal.get(Calendar.MONTH) + 1;//获取月份
        int day=cal.get(Calendar.DATE);//获取日
        int hour=cal.get(Calendar.HOUR_OF_DAY);//小时
        int minute=cal.get(Calendar.MINUTE);//分
        int second=cal.get(Calendar.SECOND);//秒
        corn = second + " " + minute + " " + hour + " " + day + " " + month + " ? " +  year;
        System.out.println(corn);
        return corn;
    }

}
